package app.tasks;

import app.agent.SearchAgent;
import app.tasks.search.End;
import app.tasks.search.SearchSignal;
import app.tasks.search.WaitMessage;
import framework.task.Task;
import jade.core.behaviours.FSMBehaviour;
import jade.core.behaviours.SimpleBehaviour;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @Name: SearchSignal
 * @Description:
 * @Author: colfish
 * @Date: 2021/5/13 20:47
 * 好好学习，天天向上！
 */
public class SearchTask extends Task {

    private static final long serialVersionUID = 8522796539357867508L;

    private final Logger logger = LogManager.getLogger(Task.class);

    // 行为种类
    private static final String searchSignal = "searchSignal";
    private static final String waitMessage = "waitMessage";
    private static final String end = "end";

    public SearchTask(){
        super();
        behaviour = new SelfBehaviour();
    }

    private class SelfBehaviour extends SimpleBehaviour {
        private static final long serialVersionUID = -317065722874315928L;
        private boolean end = false;

        @Override
        public void action() {
            myAgent.addBehaviour(create());
        }

        @Override
        public boolean done() {
            return true;
        }
    }
    private FSMBehaviour create(){
        FSMBehaviour behaviour = new FSMBehaviour();

        SearchAgent searchAgent = (SearchAgent) myAgent;

        //States
        behaviour.registerFirstState(new SearchSignal(searchAgent), searchSignal);
        behaviour.registerState(new WaitMessage(searchAgent), waitMessage);
        behaviour.registerLastState(new End(searchAgent), end);

        //Transitions
        behaviour.registerDefaultTransition(searchSignal, waitMessage);
        behaviour.registerTransition(searchSignal, waitMessage, 1);

        behaviour.registerDefaultTransition(waitMessage,waitMessage);
        behaviour.registerTransition(waitMessage, searchSignal, 2);
        behaviour.registerTransition(waitMessage, end, 0);

        return behaviour;
    }


}
